<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <!-- don't need to run this on helix -->
    <WasmCopyAppZipToHelixTestDir>false</WasmCopyAppZipToHelixTestDir>
    <SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <EnableAggressiveTrimming Condition="'$(EnableAOTAndTrimming)' != ''">$(EnableAOTAndTrimming)</EnableAggressiveTrimming>
    <PublishTrimmed Condition="'$(EnableAOTAndTrimming)' != ''">$(EnableAOTAndTrimming)</PublishTrimmed>
    <RunAOTCompilation Condition="'$(EnableAOTAndTrimming)' != ''">$(EnableAOTAndTrimming)</RunAOTCompilation>
  </PropertyGroup>

  <ItemGroup>
    <WasmExtraFilesToDeploy Include="index.html" />
    <WasmExtraFilesToDeploy Include="main.js" />
    <WasmExtraFilesToDeploy Include="appstart-frame.html" />
    <WasmExtraFilesToDeploy Include="frame-main.js" />
    <WasmExtraFilesToDeploy Include="style.css" />
    <Compile Remove="Console/Console.cs" />
  </ItemGroup>

  <Target Name="RunSample" DependsOnTargets="RunSampleWithBrowserAndSimpleServer" />

  <PropertyGroup>
    <NugetConfigContent>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
  &lt;!-- Don't use any higher level config files. --&gt;
  &lt;fallbackPackageFolders&gt;
    &lt;clear /&gt;
  &lt;/fallbackPackageFolders&gt;
  &lt;packageSources&gt;
    &lt;clear /&gt;
    &lt;add key=&quot;nuget-local&quot; value=&quot;$(ArtifactsDir)packages/$(Configuration)/Shipping/&quot; /&gt;
    &lt;add key=&quot;dotnet8&quot; value=&quot;https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json&quot; /&gt;
    &lt;add key=&quot;dotnet9&quot; value=&quot;https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json&quot; /&gt;
    &lt;add key=&quot;nuget.org&quot;  value=&quot;https://api.nuget.org/v3/index.json&quot; protocolVersion=&quot;3&quot; /&gt;
  &lt;/packageSources&gt;
    &lt;disabledPackageSources&gt;
    &lt;clear /&gt;
  &lt;/disabledPackageSources&gt;
&lt;/configuration&gt;
    </NugetConfigContent>
  </PropertyGroup>

  <Target Name="BuildWBT">
    <PropertyGroup>
      <_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
      <_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
      <_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
    </PropertyGroup>

    <Exec IgnoreExitCode="true" Command="$(_Dotnet) build $(MonoProjectRoot)wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj -c $(Configuration) -t:InstallWorkloadUsingArtifacts -p:TargetOS=browser -p:TargetArchitecture=wasm $(BuildAdditionalArgs)" />
  </Target>

  <Target Name="PrepareBlazorTemplate" Condition="!Exists('$(MonoProjectRoot)sample/wasm/blazor-frame/blazor')">
    <ItemGroup>
        <OverrideFiles Include="$(MonoProjectRoot)wasm/Wasm.Build.Tests/data/WasmOverridePacks.targets" />
        <OverrideFiles Include="$(MonoProjectRoot)wasm/Wasm.Build.Tests/data/Blazor.Directory.Build.targets" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/blazor-frame/blazor/WasmOverridePacks.targets" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/blazor-frame/blazor/Directory.Build.targets" />
    </ItemGroup>

    <MakeDir Directories="$(MonoProjectRoot)sample/wasm/blazor-frame/blazor"/>
    <Copy SourceFiles="@(OverrideFiles)" DestinationFiles="@(OverrideDestFiles)" UseSymbolicLinksIfPossible="true" OverwriteReadOnlyFiles="true" />

    <WriteLinesToFile
        File="$(MSBuildThisFileDirectory)../blazor-frame/blazor/nuget.config"
        Overwrite="true"
        Lines="$(NugetConfigContent)" />

    <Exec EnvironmentVariables="MSBuildSDKsPath=;DOTNET_ROOT=$(ArtifactsDir)bin/dotnet-latest;PATH=$(ArtifactsDir)bin/dotnet-latest:$(PATH)" WorkingDirectory="$(MSBuildThisFileDirectory)../blazor-frame/blazor" Command="dotnet new blazorwasm" />
    <Exec WorkingDirectory="$(MSBuildThisFileDirectory)../blazor-frame" Command="git apply blazor-frame.diff" />
  </Target>

  <Target Name="BuildBlazorFrame" AfterTargets="BuildSampleInTree" Condition="'$(BlazorStartup)' == 'true'" DependsOnTargets="BuildWBT;PrepareBlazorTemplate">
    <Exec EnvironmentVariables="MSBuildSDKsPath=;DOTNET_ROOT=$(ArtifactsDir)bin/dotnet-latest;PATH=$(ArtifactsDir)bin/dotnet-latest:$(PATH)" WorkingDirectory="$(MSBuildThisFileDirectory)../blazor-frame/blazor" Command="dotnet publish blazor.csproj -c $(Configuration) -p:WBTOverrideRuntimePack=true -p:TargetOS=browser -p:TargetArchitecture=wasm $(BuildAdditionalArgs)" />

    <ItemGroup>
        <BlazorSourceFiles Include="$(MSBuildThisFileDirectory)../blazor-frame/blazor/bin/$(Configuration)/$(NetCoreAppCurrent)/publish/wwwroot/blazor-template/**/*.*"/>
    </ItemGroup>

    <Copy
        SourceFiles="@(BlazorSourceFiles)"
        DestinationFolder="$(MSBuildThisFileDirectory)/bin/$(Configuration)/AppBundle/blazor-template/%(RecursiveDir)" />
  </Target>

  <Target Name="PrepareBrowserTemplate" Condition="!Exists('$(MonoProjectRoot)sample/wasm/browser-frame/browser-frame')">
    <ItemGroup>
        <OverrideFiles Include="$(MonoProjectRoot)wasm/Wasm.Build.Tests/data/WasmOverridePacks.targets" />
        <OverrideFiles Include="$(MonoProjectRoot)wasm/Wasm.Build.Tests/data/Blazor.Directory.Build.targets" />
        <OverrideFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/AppStart.cs" />
        <OverrideFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/frame.js" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/browser-frame/WasmOverridePacks.targets" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/browser-frame/Directory.Build.targets" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/browser-frame/AppStart.cs" />
        <OverrideDestFiles Include="$(MonoProjectRoot)sample/wasm/browser-frame/browser-frame/wwwroot/frame.js" />
    </ItemGroup>

    <MakeDir Directories="$(MonoProjectRoot)sample/wasm/blazor-frame/browser-frame"/>
    <Copy SourceFiles="@(OverrideFiles)" DestinationFiles="@(OverrideDestFiles)" UseSymbolicLinksIfPossible="true" OverwriteReadOnlyFiles="true" />

    <WriteLinesToFile
        File="$(MSBuildThisFileDirectory)../browser-frame/browser-frame/nuget.config"
        Overwrite="true"
        Lines="$(NugetConfigContent)" />

    <Exec EnvironmentVariables="MSBuildSDKsPath=;DOTNET_ROOT=$(ArtifactsDir)bin/dotnet-latest;PATH=$(ArtifactsDir)bin/dotnet-latest:$(PATH)" WorkingDirectory="$(MSBuildThisFileDirectory)../browser-frame/browser-frame/" Command="dotnet new wasmbrowser" />
    <Exec WorkingDirectory="$(MSBuildThisFileDirectory)../browser-frame" Command="git apply browser-frame.diff" />
  </Target>

  <Target Name="BuildBrowserFrame" AfterTargets="BuildSampleInTree" Condition="'$(BrowserStartup)' == 'true'" DependsOnTargets="BuildWBT;PrepareBrowserTemplate">

    <Exec EnvironmentVariables="MSBuildSDKsPath=;DOTNET_ROOT=$(ArtifactsDir)bin/dotnet-latest;PATH=$(ArtifactsDir)bin/dotnet-latest:$(PATH)" WorkingDirectory="$(MSBuildThisFileDirectory)../browser-frame/browser-frame" Command="dotnet publish browser-frame.csproj -c $(Configuration) -p:WBTOverrideRuntimePack=true -p:TargetOS=browser -p:TargetArchitecture=wasm $(BuildAdditionalArgs)" />

    <ItemGroup>
        <BrowserSourceFiles Include="$(MSBuildThisFileDirectory)../browser-frame/browser-frame/bin/$(Configuration)/$(NetCoreAppCurrent)/publish/wwwroot/**/*.*"/>
    </ItemGroup>

    <Copy
        SourceFiles="@(BrowserSourceFiles)"
        DestinationFolder="$(MSBuildThisFileDirectory)/bin/$(Configuration)/AppBundle/browser-template/%(RecursiveDir)" />
  </Target>
</Project>
